package com.zq.user.service.impl;

import cn.dev33.satoken.session.SaSession;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zq.user.dao.entity.UserAddress;
import com.zq.user.dao.entity.Users;
import com.zq.user.mapper.UserAddressMapper;
import com.zq.user.mapper.UsersMapper;
import com.zq.user.saconfig.UserStpUtil;
import com.zq.user.service.IUserAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户地址表 服务实现类
 * </p>
 *
 * @author 张乔
 * @since 2025-06-02
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements IUserAddressService {

    //获取用户的地址列表
    @Override
    public UserAddress findUserAddressList() {
        Object userId = UserStpUtil.getSession().getLoginId();
        if(userId==null){
            throw new RuntimeException("用户未登录");
        }
        UserAddress userAddress = this.getOne(new LambdaQueryWrapper<UserAddress>()
                .eq(UserAddress::getUserId, Integer.valueOf(userId.toString())
                )
                .eq(UserAddress::getIsDefault, 1));


        return userAddress;
    }

    //    获取用户的地址列表
    @Override
    public List<UserAddress> getAllList() {
        Object userId = UserStpUtil.getSession().getLoginId();
        if(userId==null){
            throw new RuntimeException("用户未登录");
        }
        List<UserAddress> userAddressList = this.list(new LambdaQueryWrapper<UserAddress>().
                eq(UserAddress::getUserId, Integer.valueOf(userId.toString())));

        return userAddressList;
    }
    @Resource
    private UsersMapper usersMapper;
    //    添加用户地址
    @Override
    public void saveUserAddr(UserAddress userAddress) {
        Object userId = UserStpUtil.getSession().getLoginId();
        if(userId==null){
            throw new RuntimeException("用户未登录");
        }
        Users users = usersMapper.selectById(Integer.valueOf(userId.toString()));
        userAddress.setUserId(Integer.valueOf(userId.toString()));
        userAddress.setName(users.getUsername());

        UserAddress one = this.getOne(new LambdaQueryWrapper<UserAddress>().
                eq(UserAddress::getUserId, Integer.valueOf(userId.toString()))
                .eq(UserAddress::getIsDefault, 1)
        );
        if (one==null){
//            用户没有默认地址
            this.save(userAddress);
            return;
        }
        if (one.getIsDefault()==1 && 1==userAddress.getIsDefault()){
            throw new RuntimeException("默认地址已经存在");
        }


        this.save(userAddress);
    }
    //    修改用户地址
    @Override
    public void updateByIdUserAddr(UserAddress userAddress) {
        Object userId = UserStpUtil.getSession().getLoginId();
        if(userId==null){
            throw new RuntimeException("用户未登录");
        }
        UserAddress one = this.getOne(new LambdaQueryWrapper<UserAddress>().
                eq(UserAddress::getUserId, Integer.valueOf(userId.toString()))
                .eq(UserAddress::getIsDefault, 1)
        );
        if (one==null){
//            用户没有默认地址
            this.updateById(userAddress);
            return;
        }
        if (one.getIsDefault()==1 && 1==userAddress.getIsDefault()
                && !Objects.equals(one.getId(), userAddress.getId())){
            throw new RuntimeException("默认地址已经存在");
        }
        this.updateById(userAddress);


    }
}
